package com.pay.ioopos;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.pay.ioopos.sqlite.SettingHelper;

public class DataStoreSetting implements DataStore {
    private SettingHelper helper;

    public DataStoreSetting(Context context) {
        helper = new SettingHelper(context, "setting.db", null, 9);
    }

    @Override
    public void set(String name, String value) {
        SQLiteDatabase wdb = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("value", value);
        int count = wdb.update("setting", cv, "name=?", new String[]{name});
        if (count == 0) {
            cv.put("name", name);
            wdb.insert("setting", null, cv);
        }
    }

    @Override
    public String get(String name) {
        SQLiteDatabase rdb = helper.getReadableDatabase();
        Cursor c = rdb.rawQuery("select value from setting where name=?", new String[]{name});
        if (c.moveToNext()) {
            return c.getString(c.getColumnIndex("value"));
        }
        return null;
    }

    @Override
    public void setSuperPwd(String superPwd) {
        set("superPwd", superPwd);
    }

    @Override
    public String getSuperPwd() {
        return get("superPwd");
    }

    @Override
    public void setMode(PayMode mode) {
        set("mode", "" + mode.ordinal());
    }

    @Override
    public PayMode getMode() {
        String mode = get("mode");
        if (null == mode || mode.isEmpty()) {
            mode = "0";
        }
        return PayMode.values()[Integer.parseInt(mode)];
    }

    @Override
    public void setMaxAmount(String amount) {
        set("maxAmount", amount);
    }

    @Override
    public String getMaxAmount() {
        return get("maxAmount");
    }

    @Override
    public void setFixAmount(String fixedAmount) {
        set("fixAmount", fixedAmount);
    }

    @Override
    public String getFixAmount() {
        return get("fixAmount");
    }

    @Override
    public void setPwd(String pwd) {
        set("pwd", pwd);
    }

    @Override
    public String getPwd() {
        return get("pwd");
    }

    @Override
    public void setTransKey(String transKey) {
        set("transKey", transKey);
    }

    @Override
    public String getTransKey() {
        return get("transKey");
    }

    @Override
    public void setServerUrl(String serverUrl) {
        set("serverUrl", serverUrl);
    }

    @Override
    public String getServerUrl() {
        return get("serverUrl");
    }

    @Override
    public void setMerchNo(String merchNo) {
        set("merchNo", merchNo);
    }

    @Override
    public String getMerchNo() {
        return get("merchNo");
    }

    @Override
    public void setTerminalNo(String terminalNo) {
        set("terminalNo", terminalNo);
    }

    @Override
    public String getTerminalNo() {
        return get("terminalNo");
    }

    @Override
    public void setTransPrefix(String transPrefix) {
        set("transPrefix", transPrefix);
    }

    @Override
    public String getTransPrefix() {
        return get("transPrefix");
    }

    @Override
    public void setCableIp(String cableIp) {
        set("cableIp", cableIp);
    }

    @Override
    public String getCableIp() {
        return get("cableIp");
    }

    @Override
    public void setSwitchScanPay(boolean bool) {
        set("switchScanPay", "" + bool);
    }

    @Override
    public boolean getSwitchScanPay() {
        return Boolean.parseBoolean(get("switchScanPay"));
    }

    @Override
    public void setSwitchIcPay(boolean bool) {
        set("switchIcPay", "" + bool);
    }

    @Override
    public boolean getSwitchIcPay() {
        return Boolean.parseBoolean(get("switchIcPay"));
    }

    @Override
    public void setSwitchScanRefund(boolean bool) {
        set("switchScanRefund", "" + bool);
    }

    @Override
    public boolean getSwitchScanRefund() {
        return Boolean.parseBoolean(get("switchScanRefund"));
    }
}
